<?php
namespace app\beautiful\controller\admin\member;
use app\BaseController;
use app\common\logic\DataLogic;
use app\beautiful\logic\LogsLogic;
use app\beautiful\logic\MemberWalletLogic;
use app\beautiful\model\logs\MemberSupplementModel;
use app\beautiful\model\member\MemberModel;
use think\facade\Db;

class Supplement extends BaseController{
    public function index()
    {
        $nick_name=$this->request->param('nick_name');
        $mobile=$this->request->param('mobile');
        $where=[
            ['shop_id','=',$this->request->shopId],
        ];
        $member_where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]
        ];
        if(!empty($nick_name)) $member_where[]=['nick_name','=',$nick_name];
        if(!empty($mobile)) $member_where[]=['mobile','=',$mobile];
        $member_ids=MemberModel::where($member_where)->column('member_id') ?? [];
        $where[]=['member_id','in',$member_ids];

        $supplement_model= new MemberSupplementModel();
        $supplement_model=$supplement_model->with(['member']);
        $order=['add_time'=>'desc'];
        $field='*';
        $append=['add_time_format'];

        $return = DataLogic::getDataList($supplement_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function getMemberWithMobileFromSupplement()
    {
        $mobile = $this->request->param('mobile');
        if(empty($mobile) || !is_mobile($mobile)){
            return $this->error('请输入手机号');
        }

        if (null === $member = MemberModel::where([
                ['mobile','=',$mobile],
                ['shop_id','=',$this->request->shopId],
                ['status','=',1],
                ['is_delete','=',0]
            ])->find()) {
            return $this->error('会员不存在');
        }
        $member->toArray();
        return $this->success(['member'=>$member]);
    }

    public function save()
    {
        $member_id = $this->request->param('member_id/d',0);
        $money=$this->request->param('money/f',0);
        if($member_id==0) return $this->error('请选择会员');
        if($money<=0) return $this->error('请输入金额，且金额不能小于等于0');

        if(null === $member = MemberModel::find($member_id)) return $this->error('会员不存在');
        if($member->shop_id != $this->request->shopId) return $this->error('会员不存在');
        if($member->is_delete != 0 ) return $this->error('会员不存在');

        Db::startTrans();
        try{
            //先充值
            $MemberWalletLogic = new MemberWalletLogic($this->request->shopId, $member_id);
            $res = $MemberWalletLogic->addBalance($money, 0, 10,'', $this->request->accountId);
            if($res == false){
                Db::rollback();
                return $this->error($MemberWalletLogic->getError());
            }

            //在消耗
            $res = $MemberWalletLogic->useBalance($money, $money, 11,'', $this->request->accountId);
            if($res == false){
                Db::rollback();
                return $this->error($MemberWalletLogic->getError());
            }

            //插入一条记录
            $res=LogsLogic::supplement($this->request->shopId,$member->member_id,2,$money,$this->request->accountId);
            if(!$res){
                Db::rollback();
                return $this->error('操作失败');
            }

            Db::commit();
            return $this->success('补单成功');


        }catch (\Exception $e){
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }
}
